﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;

namespace sands
{
    public class PrintTotalDeposits:SandsPrintDocument
    {
        protected List<String[,]> listOfDeposits;

        public PrintTotalDeposits(String date, List<String[,]> input)
            : base(date)
        {
            listOfDeposits = input;
            columnsNames = new String[11];
            columns = new Rectangle[columnsNames.Length];
        }

        protected override void DrawTitle(ref int top)
        {
              Font = new Font("Veranda", 8);

              //heading
              Image img = Image.FromFile("logo_sands.bmp");
              gdiPage.DrawImage(img, new Rectangle(top,100,img.Width,img.Height));
              top += img.Height;
              gdiPage.DrawString("SMITH & SCHMIDT\nszkoła języków obcych\nKu Studzionkom 12\n34-400 Nowy Targ", Font, Brushes.Black, new Point(100, top));
              top += 4 * Font.Height; //it takes 3 lines to display above text

              top += 2 * Font.Height; //2 lines down

              //title
              String tmp = "Wykaz wpłat na dzień "+date;
              gdiPage.DrawString(tmp, Font, Brushes.Black, 425 - gdiPage.MeasureString(tmp, Font).Width / 2,top);

             top += 2 * Font.Height; //2 lines down
        }

        protected override void DrawHeaders(ref int top)
        {
            //column headers
            columnsNames[0] = "Nazwisko\nImię\nIndex";
            for (int i = 1; i < columnsNames.Length - 1; i++)
                columnsNames[i] = i.ToString();

            columnsNames[columnsNames.Length - 1] = "Razem";

            //columns
            columns[0].Location = new Point(100, top);
            columns[0].Size = new Size((int)gdiPage.MeasureString(columnsNames[0].Split('\n')[0], Font).Width + 20, 3 * Font.Height); //note:: last line, different font

            //headers row
            for (int i = 1; i < columnsNames.Length; i++)
                columns[i] = new Rectangle(columns[i - 1].Right, top, (int)gdiPage.MeasureString(columnsNames[i], Font).Width + 41, columns[0].Height);

            //background color of the headers
            gdiPage.FillRectangles(Brushes.Wheat, columns);

            //first cell (3 lines)
            gdiPage.DrawString(columnsNames[0].Split('\n')[0], Font, Brushes.Black, 100, top);
            top += Font.Height;
            gdiPage.DrawString(columnsNames[0].Split('\n')[1], Font, Brushes.Black, 100, top);
            top += Font.Height;
            gdiPage.DrawString(columnsNames[0].Split('\n')[2], new Font("Veranda", 7), Brushes.Black, 100, top);

            top -= 2 * Font.Height; //go up

            //print the rest of teh strings
            for (int i = 1; i < columnsNames.Length; i++)
                gdiPage.DrawString(columnsNames[i], Font, Brushes.Black, columns[i]);

            //print the columns
            gdiPage.DrawRectangles(Pens.Black, columns);

            top += columns[0].Height;//go to the next row
        }

        protected override bool DrawTable(ref int top)
        {
            System.Diagnostics.Debug.WriteLine("rowNumber " + rowNumber);
              while(rowNumber<listOfDeposits.Count)
              {
                  //create first cell
                  Rectangle cell = new Rectangle(100, top,columns[0].Width,columns[0].Height);

                  //print Name and Index
                  gdiPage.DrawString(listOfDeposits[rowNumber][0,0], Font, Brushes.Black, 100,top);
                  top += Font.Height;
                  gdiPage.DrawString(listOfDeposits[rowNumber][0, 1], Font, Brushes.Black, 100, top);
                  top += Font.Height;
                  gdiPage.DrawString(listOfDeposits[rowNumber][0, 2], new Font("Veranda", 7), Brushes.Black, 100, top);
                
                  //print first cell
                  gdiPage.DrawRectangle(Pens.Black, cell);

                  top -= 2 * Font.Height; //go up

                  //print the deposits
                  for (int i = 3; i < listOfDeposits[rowNumber].GetLength(1); i++)
                  {
                      //made
                      cell = new Rectangle(cell.Right, top, columns[i-2].Width, columns[0].Height);

                      gdiPage.DrawString(listOfDeposits[rowNumber][0, i], Font, Brushes.Black, Align(listOfDeposits[rowNumber][0, i],cell,AlignType.Right));
                      gdiPage.DrawRectangle(Pens.Black, cell);

                      //scheduled
                      cell = new Rectangle(cell.Left, top+2*Font.Height, columns[i - 2].Width, columns[0].Height-2*Font.Height);

                      if(listOfDeposits[rowNumber][1,0] == "late")
                        gdiPage.FillRectangle(Brushes.Red, cell);
                      else
                        gdiPage.FillRectangle(Brushes.Beige, cell);

                      gdiPage.DrawRectangle(Pens.Black, cell);
                      gdiPage.DrawString(listOfDeposits[rowNumber][1, i], Font, Brushes.Black, Align(listOfDeposits[rowNumber][1, i], cell,AlignType.Right));
                  }

                  top += columns[0].Height; //go to the next row
                  ++rowNumber;

                  if (top > 1000) //more than one page
                      return true;
              }

              return false;
        }
    }
}
